package ipt.capitulo19;
import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.io.File;
public class PreVisualizarImagen extends JComponent 
								implements PropertyChangeListener{
	ImageIcon imagen = null;
	File fichero = null;
	public PreVisualizarImagen(JFileChooser fc){
		setPreferredSize(new Dimension(100, 50));
		fc.addPropertyChangeListener(this);
	}
	public void cargaImagen(){
		if (fichero == null)
			return;
		ImageIcon aux = new ImageIcon(fichero.getPath());
		if (aux.getIconWidth() > 90) 
			imagen = new ImageIcon(aux.getImage().
			getScaledInstance(90, -1, Image.SCALE_DEFAULT));
		else
			imagen = aux;
	}
	public void propertyChange(PropertyChangeEvent ev){
		String prop = ev.getPropertyName();
		if(prop.equals(
				JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)){
			fichero = (File)ev.getNewValue();
			if (isShowing()){
				cargaImagen();
				repaint();
			}
		}
	}
	public void paintComponent(Graphics g){
		if(imagen != null) {
			int x = getWidth()/2 - imagen.getIconWidth()/2;
			int y = getHeight()/2 - imagen.getIconHeight()/2;
			if (y < 0) y = 0;
			if (x < 5) x = 5;
			imagen.paintIcon(this, g, x, y);
		}else
			cargaImagen();
	}
}
